/*
创建时间: 2020/10/28
作者: Administrator
功能介绍:
*/
package main

import (
	"ResServer/apis"
	"ResServer/global"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
	"os"
	"path/filepath"
)

//func GetName(c *gin.Context){
//	name := c.("name","xxx")
//	action := c.Query("action")
//	message := name + " is " + action
//	c.String(http.StatusOK, message)
//	c.Cookie()
//	//TestRedis()
//}
//
//
//func   PostName(c *gin.Context){
//	name:= c.DefaultPostForm("name","xxx")
//	lastname:= c.DefaultPostForm("lastname","xxx")
//	qqq := c.PostForm("qqq")
//	c.JSON(http.StatusOK,gin.H{
//		"name":name,
//		"lastname":lastname,
//		"qqq":qqq,
//	})
//}
//
//func   uploadfile(c *gin.Context){
//	fh,erro := c.FormFile("file")
//	fmt.Printf(fh.Filename)
//	if erro != nil {
//		c.JSON(http.StatusForbidden,gin.H{
//			"erro" : erro,
//		})
//		return
//	}
//	c.SaveUploadedFile(fh,"./templates/c.txt")
//}
//
//func   DowloadFile(c *gin.Context){
//	c.Writer.Header().Add("Content-Disposition",`attachment;filename="c.rar"`)//fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
//	//c.Writer.Header().Add("Content-Type", "application/octet-stream")
//	c.Writer.Header().Add("Content-Type", "application/zip")
//	c.File("./templates/c.rar")
//}

func TestPost(c *gin.Context) {
	message := c.PostForm("data")
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": message,
	})
}

func TestGet(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "aaa",
	})
}

func TestPut(c *gin.Context) {
	message, _ := io.ReadAll(c.Request.Body)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": string(message),
	})
}

func main() {
	jsonfile, erro := os.Open("appinfo.json")
	if erro != nil {
		fmt.Println(erro)
		return
	}
	jsond := json.NewDecoder(jsonfile)
	if jsond == nil {
		fmt.Printf("json decoder 失败")
		return
	}
	jsond.Decode(&apis.GAppInfo)
	filepath.Walk(apis.GAppInfo.JsonCfgPath, apis.WalkOneFile)

	apis.LoadVersion()
	global.G_ginEngine.LoadHTMLGlob("templates/*")
	global.G_ginEngine.StaticFS("/assets", gin.Dir(apis.GAppInfo.RelativePath, true))
	global.G_ginEngine.GET("/reload", apis.RLoadVersion)
	global.G_ginEngine.GET("/getServerInfo", apis.GetServerInfo)
	global.G_ginEngine.POST("/uploadls", apis.UploadLogin)
	global.G_ginEngine.GET("/reloadCfg", apis.LoadJsonCfg)
	global.G_ginEngine.POST("/TestPost", TestPost)
	global.G_ginEngine.GET("/Test", TestGet)
	global.G_ginEngine.GET("/", apis.Home)
	global.G_ginEngine.Run(":" + apis.GAppInfo.HttpPort)
}
